package memorphic.samplr.utils
{
	import flash.display.BitmapData;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Rectangle;
	
	import memorphic.samplr.models.SamplrPhoto;
	
	public class TempPhotoCache
	{
		public static const TEMP_CACHE_DIR:File = File.applicationStorageDirectory.resolvePath("tempPhotoCache");
		
		public function TempPhotoCache():void
		{
		}
		
		public static function writeToTempCache(photo:SamplrPhoto, bitmapData:BitmapData):void
		{
			var f:File = TEMP_CACHE_DIR.resolvePath(photo.uniqueID + ".jpg");
			var fs:FileStream = new FileStream();
			fs.open(f,FileMode.WRITE);
			fs.writeObject(bitmapData);
			fs.close();
		}
		
		public static function clearCache():void
		{
			if(TEMP_CACHE_DIR.exists)
			{
				TEMP_CACHE_DIR.deleteDirectory(true);
			}
		}
		
		public static function readFromTempCache(photo:SamplrPhoto):BitmapData
		{
			var f:File = TEMP_CACHE_DIR.resolvePath(photo.uniqueID + ".jpg");
			var fs:FileStream = new FileStream();
			fs.open(f,FileMode.READ);
			var obj:Object = fs.readObject();
			var bmd:BitmapData = BitmapData(obj);
			fs.close();
			return bmd;
		}
	}
}